package com.zhuodewen.ai.util;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import java.util.Locale;

/**
 * 国际化/多语言(i18n)工具类
 */
@Component
public class InternationalUtils {

    private static MessageSource messageSource;

    public InternationalUtils(MessageSource messageSource) {
        this.messageSource = messageSource;
    }

    /**
     * 获取国际化/多语言的实际返回值
     *
     * @param key
     * @return
     */
    public static String getInternational(String key) {
        try {
            Locale locale = LocaleContextHolder.getLocale();
            if (ObjectUtil.isEmpty(locale) || StrUtil.isEmpty(locale.toString())) {
                locale = Locale.CHINA;
            }
            return messageSource.getMessage(
                    key,
                    null,
                    locale);
        } catch (Exception e) {
            return key;
        }
    }

}